package cn.mothin.frame.help.config;

import java.io.File;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.logging.LogFactory;

import cn.mothin.frame.core.exception.SystemException;

public class ConfigReader {
	private static ConfigReader attachConfigReader;

	public static ConfigReader getInstance() {
		if (attachConfigReader == null) {
			attachConfigReader = new ConfigReader();
		}
		return attachConfigReader;
	}

	private Configuration config = null;

	public Configuration getConfiguration() {
		return config;
	}

	public ConfigReader() {
		try {
			config = new PropertiesConfiguration(CONFIG_FILE);
		} catch (ConfigurationException e) {
			LogFactory.getLog(ConfigReader.class).error(e);
			throw new SystemException();
		}
	}

	// 文件路径
	protected static final String CONFIG_FILE = "/frame.properties";
	// MD5加密次数设置
	private static final String PROPERTY_NAME_MD5_TIME = "md5Time";
	// Id生成设置
	private static final String PROPERTY_NAME_GENERATE_UNIQUE_CODE_MODE = "generateUniqueCodeMode";
	// 操作详细是否需要记录
	private static final String PROPERTY_NAME_RECORD_OPERATION_DETAIL = "recordOperationDetail";
	// 记录系统启动时间
	private static final String PROPERTY_NAME_RECORD_RESET_TIME = "recordResetTime";
	
	// 附件文件存贮目录
	private static final String CONFIG_ITEM_ATTACH_ROOT = "attachment_root";

	private static final String DEFAULT_ATTATCH_ROOT = "attach/";

	/**
	 * 获取配置的附件存贮根目录
	 * 
	 * @return
	 */
	public String getAttachRoot() {
		String path = config.getString(CONFIG_ITEM_ATTACH_ROOT);
		if (path == null || path.trim().length() == 0) {
			return DEFAULT_ATTATCH_ROOT;
		}
		if (!path.endsWith("/")) {
			path += "/";
		}
		return path;
	}
	
	public String makeFolder(String name){
		Date now = new Date(System.currentTimeMillis());
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(now);
		
		String root = getAttachRoot()+name+"/";
		String year = String.valueOf(calendar.get(Calendar.YEAR));
		String month = String.valueOf(calendar.get(Calendar.MONTH));
		
		String path = year + "/" + month + "/";
		
		File folder = new File(root + path);
		if (!folder.exists()) {
			folder.mkdirs();
		}
		
		return root + path;
	}
	
	
	/**
	 * 获取配置的加密次数
	 * 
	 * @return
	 */
	public int getMD5Time() {
		int time = config.getInt(PROPERTY_NAME_MD5_TIME);
		if (time < 1) {
			time = 1;
		}
		return time;
	}

	public int getGenerateUniqueCodeMode() {
		int mode = config.getInt(PROPERTY_NAME_GENERATE_UNIQUE_CODE_MODE);
		if (mode != 0 && mode != 1) {
			mode = 0;
		}
		return mode;
	}

	/**
	 * 获取是否记录详细修改信息配置
	 * 
	 * @return
	 */
	public boolean doRecordOperationDetail() {
		return config.getBoolean(PROPERTY_NAME_RECORD_OPERATION_DETAIL);
	}

	/**
	 * 获取是否记录系统启动时间
	 * 
	 * @return
	 */
	public boolean doRecordResetTime() {
		return config.getBoolean(PROPERTY_NAME_RECORD_RESET_TIME);
	}
	
	/**
	 * 权限视图模式
	 * @return
	 */
	public int getViewMode(){
		return config.getInt("viewMode",0);
	}
}
